<?php
/**
 * Created by PhpStorm.
 * User: dreamy
 * Date: 2019-06-22
 * Time: 09:31
 */
use yii\helpers\Html;
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title><?= \common\services\DomainExtraConfigService::getVal('name');?></title>
    <meta name="keywords" content="<?= $this->params['seoData']['keywords'] ?>">
    <meta name="description" content="<?= $this->params['seoData']['description'] ?>">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/global.css">
    <style>
        .el-loading-spinner{
            top:10%
        }
        #app {
            padding-right:0px!important;
        }
        .pMain{
            display: block;
            background: #fff;
            margin-left: 10px;
            padding: 20px 24px;
            min-height: 700px;
            border-radius: 0.25rem;
            margin:20px;
        }
        .pMain1{
            display: block;
            min-height: 700px;
            border-radius: 0.25rem;
        }

    </style>
</head>

<body id="app" style="margin-top: 0px;overflow-y: hidden;overflow-x: auto;position: relative;">

<?=Html::cssFile('@web/static/css/index.css')?>
<?php echo $this->render('../public/ele.php')?>
<!--引入账本样式-->
<?=Html::cssFile('@web/static/css/tally.css?v=20220215')?>
<?=Html::jsFile('@web/static/js/jquery-3.4.1.min.js')?>
<?=Html::jsFile('@web/static/js/highcharts.js')?>
<?=Html::jsFile('@web/static/js/tally-charts.js?v=1')?>
<?=Html::jsFile('@web/static/js/china-area-data.js?v=1')?>

<!--引入模态组-->
<?php echo $this->render('component/dialog/account_form')?>
<?php echo $this->render('component/dialog/category_form')?>
<?php echo $this->render('component/dialog/project_form')?>
<?php echo $this->render('component/dialog/member_form')?>
<?php echo $this->render('component/dialog/code_tally')?>
<?php echo $this->render('component/dialog/buy_vip_dialog')?>
<?php echo $this->render('component/dialog/day_dialog')?>
<?php echo $this->render('component/dialog/store_form')?>
<?php echo $this->render('component/dialog/tally_form')?>
<?php echo $this->render('component/dialog/binduser_form')?>
<?php echo $this->render('component/dialog/record_form')?>
<?php echo $this->render('component/dialog/future_form')?>
<?php echo $this->render('component/dialog/arap_form')?>
<?php echo $this->render('component/dialog/bind_projects')?>
<?php echo $this->render('component/dialog/bind_accounts')?>
<?php echo $this->render('component/dialog/arap_details')?>
<?php echo $this->render('component/dialog/future_details')?>
<?php echo $this->render('component/dialog/record_details_report')?>
<?php echo $this->render('component/dialog/future_details_report')?>
<?php echo $this->render('component/dialog/export_dialog.php')?>
<?php echo $this->render('component/dialog/arap_details_report')?>
<?php echo $this->render('component/dialog/stock_product_form')?>
<?php echo $this->render('component/dialog/stock_warehouse_form')?>
<?php echo $this->render('component/dialog/stock_balance_form')?>
<?php echo $this->render('component/dialog/stock_sales_form')?>
<?php echo $this->render('component/dialog/stock_balance_product_details')?>
<?php echo $this->render('component/dialog/stock_balance_house_details')?>
<?php echo $this->render('component/dialog/stock_record_product_details')?>
<?php echo $this->render('component/dialog/product_stock_case')?>
<?php echo $this->render('component/dialog/edit_uid_tally')?>
<?php echo $this->render('component/dialog/report_export')?>
<?php echo $this->render('component/dialog/material_form')?>
<?php echo $this->render('component/dialog/record_material_form.php')?>
<?php echo $this->render('component/dialog/look_material_details.php')?>

<!--引入顶部Header组件-->
<?php echo $this->render('component/header.php')?>
<!--引入账户组件-->
<?php echo $this->render('component/account.php')?>
<!--引入应收付组件-->
<?php echo $this->render('component/arap.php')?>
<!--引入预收预付-->
<?php echo $this->render('component/future.php')?>
<!--引入账单流水组件-->
<?php echo $this->render('component/bill.php')?>
<!--引入报表统计组件-->
<?php echo $this->render('component/report.php')?>
<!--引入报表统计组件-->
<?php echo $this->render('component/reportold.php')?>
<!--引入分类组件-->
<?php echo $this->render('component/category.php')?>
<!--引入商家组件-->
<?php echo $this->render('component/store.php')?>
<!--引入项目组件-->
<?php echo $this->render('component/project.php')?>
<!--引入成员组件-->
<?php echo $this->render('component/member.php')?>

<!--引入材料管理-->
<?php echo $this->render('component/material.php')?>
<!--引入设置组件-->
<?php echo $this->render('component/setting.php')?>
<!--引入记一笔组件-->
<?php echo $this->render('component/write.php')?>
<!--引入进销存组件-->
<?php echo $this->render('component/stock/stock.php')?>
<?php echo $this->render('component/stock/product.php')?>
<?php echo $this->render('component/stock/warehouse.php')?>
<?php echo $this->render('component/stock/manage.php')?>
<?php echo $this->render('component/stock/sales.php')?>
<?php echo $this->render('component/stock/warehouse1.php')?>

<div id="mainContent" style="display: none;" :style="{display: 'block'}">
    <!--顶部topbar-->
    <tally-header></tally-header>
    <!--主页面布局-->
    <el-container v-loading="initLoading" style="background:#f5f6fa;">
        <!--侧边导航栏-->
        <el-aside width="200px" style="background:#fff;">
            <el-menu :router="true" :default-active="openMenu">
                <el-menu-item index="write">
                    <i class="el-icon-edit"></i>
                    <span slot="title">记一笔</span>
                </el-menu-item>
                <el-menu-item index="arap">
                    <i class="el-icon-collection"></i>
                    <span slot="title">应收/应付</span>
                </el-menu-item>
                <el-menu-item index="future">
                    <i class="el-icon-s-check"></i>
                    <span slot="title">预收/预付</span>
                </el-menu-item>
                <el-menu-item index="stock" v-if="userData['vip_status'] == 0">
                    <i class="el-icon-coin"></i>
                    <span slot="title">进销存</span>
                </el-menu-item>
                <el-menu-item index="bill">
                    <i class="el-icon-s-order"></i>
                    <span slot="title">账单流水</span>
                </el-menu-item>
                <el-menu-item index="report" v-if="userData['vip_status'] == 1">
                    <i class="el-icon-s-marketing"></i>
                    <span slot="title">财务报表</span>
                </el-menu-item>

                <el-menu-item index="material" v-if="userData['vip_status'] == 1">
                    <i class="el-icon-s-opportunity"></i>
                    <span slot="title">材料管理</span>
                </el-menu-item>

                <el-menu-item index="reportold" v-if="userData['vip_status'] == 0">
                    <i class="el-icon-s-marketing"></i>
                    <span slot="title">财务报表</span>
                </el-menu-item>
                <el-menu-item index="category" v-if="cbuildShowVer()">
                    <i class="el-icon-s-operation"></i>
                    <span slot="title">分类管理</span>
                </el-menu-item>
                <el-menu-item index="account" v-if="cbuildShowVer()">
                    <i class="el-icon-s-finance"></i>
                    <span slot="title">账户管理</span>
                </el-menu-item>
                <el-menu-item index="project" v-if="cbuildShowVer()">
                    <i class="el-icon-c-scale-to-original"></i>
                    <span slot="title">项目管理</span>
                </el-menu-item>
                <el-menu-item index="store" v-if="cbuildShowVer()">
                    <i class="el-icon-s-shop"></i>
                    <span slot="title">客户/供应商</span>
                </el-menu-item>
                <el-menu-item index="member">
                    <i class="el-icon-s-custom"></i>
                    <span slot="title">员工管理</span>
                </el-menu-item>
                <el-menu-item index="setting" v-if="cbuildShowVer()">
                    <i class="el-icon-setting"></i>
                    <span slot="title">账本设置</span>
                </el-menu-item>
            </el-menu>
        </el-aside>
        <!--路由容器-->
        <el-container class="pMain" v-loading="initLoading" v-if="openMenu != 'report'">
            <router-view v-if="!initLoading"></router-view>
        </el-container>

        <el-container class="pMain1"  v-loading="initLoading" v-else>
             <router-view v-if="!initLoading"></router-view>
       </el-container>
    </el-container>
</div>

<?php echo $this->render('../public/footer.php')?>
<!--引入vuex store-->
<?php echo $this->render('component/store/whole.php')?>
<!--引入router-->
<?php echo $this->render('component/router/index.php')?>
<script>
    let activeTabTop = '<?php echo $this->params['activeTab'];?>'
    let CurrDateR = '<?php echo date('Y-m-d');?>';
    let CurrTimeR = '<?php echo date('H:i:s');?>';
    let monthBeginDate = '<?php echo $monthBeginDate;?>';
    let monthEndDate   = '<?php echo $monthEndDate;?>';
    let currentTallyId = '<?php echo $tallyId?>';//当前选中状态的账本
    let loginToken  =  localStorage.getItem('loginToken');

    new Vue({
        el:'#mainContent',
        router: xRouter,
        store: xStore,
        data() {
            return {
                activeTabTop:activeTabTop,
                initLoading:true,
                emptyFirst:true,
            }
        },
        computed: {
            currentTallyId() {
                return this.$store.state.currentTallyId
            },
            userData () {
                return this.$store.state.userData
            },
            currentTally () {
                return this.$store.state.currentTally // 从store中获取实时数据
            },
            openMenu() {
                let routerKey = this.$route.path.substr(1)
                if(routerKey === '') routerKey = 'write'
                return routerKey
            }
        },
        mounted:function() {
            let that = this;
            that.initLoading = true;
            that._initLogin()
                .then(()=>{
                    that._initTally()
                })
                .catch(()=>{
                    that.$message({
                        type: 'error',
                        message: '账套初始化失败~请刷新后重试'
                    });
                    setTimeout(function () {
                        window.location.href='/login';
                    },2000)
                })
        },
        methods: {
            cbuildShowVer(){
                if(this.userData['vip_status'] == 1){
                    if(this.currentTally['role'] == 'pmanger'){
                        return false;
                    }
                }

                return true;
            },
            _initLogin (){
                let that = this;
                return new Promise(
                    function (resolve, reject) {
                        const loginToken = localStorage.getItem('loginToken');
                        if(loginToken){
                            that.getUserByToken(loginToken)
                                .then((userData)=>{
                                    localStorage.setItem('userInfo',JSON.stringify(userData));
                                    that.$store.commit('setUserData',userData);
                                    that.userData = userData;
                                    resolve()
                                })
                                .catch(()=>{
                                    localStorage.removeItem('userInfo');
                                    localStorage.removeItem('loginToken');
                                    reject()
                                })
                        }else{
                            localStorage.removeItem('userInfo');
                            reject()
                        }
                    }
                )
            },
            getUserByToken(token) {
                return new Promise(function (resolve, reject) {
                    axios.get('/api/user/get-info',{
                        method: 'get',
                        headers: {'token': token},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            const userData = res.data.data;
                            resolve(userData)
                        }else{
                            reject()
                        }
                    });
                })
            },
            _initTally(){
                let that = this;
                this.getTallyList().then(
                    function(){
                        Promise.all([
                            that.$store.dispatch('getCategoryListBase',{
                                tallyId:that.currentTallyId,
                                cateType:1
                            }),
                            that.$store.dispatch('getCategoryListBase',{
                                tallyId:that.currentTallyId,
                                cateType:2
                            }),
                            that.$store.dispatch('getAccountList',that.currentTallyId),
                            that.$store.dispatch('getProjectList',that.currentTallyId),
                            that.$store.dispatch('getMemberList',that.currentTallyId),
                            that.$store.dispatch('getMaterialList',that.currentTallyId),

                            that.$store.dispatch('getStoreList',{
                                tallyId:that.currentTallyId,
                                type:1
                            }),
                            that.$store.dispatch('getStoreList',{
                                tallyId:that.currentTallyId,
                                type:2
                            }),
                        ]).then(function () {
                            console.log('账本数据初始化完成');
                            that._initOther();
                            that.initLoading = false
                        })
                    }
                )
            },
            _initOther(){
                this.$store.dispatch('getAccountTags',this.currentTallyId)
                this.$store.dispatch('getIndustryOptions')
            },
            showTips(type,msg){
                this.$message({
                    type: type,
                    message: msg
                });
            },
            getTallyList(){
                let that = this
                return new Promise(
                    function (resolve, reject) {
                        that.$store.dispatch('getTallyList').then((list)=>{
                            if(list.length > 0) {
                                if(parseInt(currentTallyId) === 0) {
                                    that.$store.commit('setTally',list[0])
                                    that.$store.dispatch('checkTallyById',list[0]['id']);
                                }else{
                                    list.forEach((item)=>{
                                        if(parseInt(item.id)=== parseInt(currentTallyId)) {
                                            that.$store.commit('setTally',item)
                                            that.$store.dispatch('checkTally');
                                        }
                                    })
                                }
                            }else{
                                if(that.emptyFirst) {
                                    that.showTips('warning','初始化加载中~')
                                    that.emptyFirst = false
                                    setTimeout(function () {
                                        that.getTallyList()
                                    },5000)
                                }else{
                                    that.showTips('warning','还没有账本~ 先创建一个吧~')
                                    setTimeout(function () {
                                        location.href='/tallyList'
                                    },1000)
                                }
                            }
                            setTimeout(function () {
                                resolve()
                            },300)
                        }).catch(()=>{
                            reject()
                        })
                    }
                )
            },
        }
    })
</script>
</body>
</html>
